// EXDialog.cpp : implementation file
//

#include "stdafx.h"
#include "EXDialog.h"
#include "afxdialogex.h"


// CEXDialog dialog

IMPLEMENT_DYNAMIC(CEXDialog, CDialog)

CEXDialog::CEXDialog()
{
  m_bImg = FALSE;
  m_crBkgnd = RGB(109, 111, 134);
  m_crText = RGB(188, 191, 216);
  m_crCaption = RGB(174, 175,193);
  m_strCaption.Empty();
  m_pftText = NULL;
}

CEXDialog::CEXDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
: CDialog(lpszTemplateName, pParentWnd)
{
  m_bImg = FALSE;
  m_crBkgnd = RGB(109, 111, 134);
  m_crText = RGB(188, 191, 216);
  m_crCaption = RGB(174, 175,193);
  m_strCaption.Empty();
  m_pftText = NULL;
}

CEXDialog::CEXDialog(UINT nIDTemplate, CWnd* pParentWnd)
  : CDialog(nIDTemplate, pParentWnd)
{
  m_bImg = FALSE;
  m_crBkgnd = RGB(109, 111, 134);
  m_crText = RGB(188, 191, 216);
  m_crCaption = RGB(174, 175,193);
  m_strCaption.Empty();
  m_pftText = NULL;
}

CEXDialog::~CEXDialog()
{
}

void CEXDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CEXDialog, CDialog)
  ON_WM_NCHITTEST()
END_MESSAGE_MAP()


// CEXDialog message handlers

void CEXDialog::InitBitmap(CEXDialogResSet* pResSet)
{
  m_bmpDlgT.LoadBitmap(pResSet->m_arrImgId.GetAt(0));
  m_bmpDlgB.LoadBitmap(pResSet->m_arrImgId.GetAt(1));
  m_bmpDlgL.LoadBitmap(pResSet->m_arrImgId.GetAt(2));
  m_bmpDlgR.LoadBitmap(pResSet->m_arrImgId.GetAt(3));
  m_bmpDlgLT.LoadBitmap(pResSet->m_arrImgId.GetAt(4));
  m_bmpDlgLB.LoadBitmap(pResSet->m_arrImgId.GetAt(5));
  m_bmpDlgRT.LoadBitmap(pResSet->m_arrImgId.GetAt(6));
  m_bmpDlgRB.LoadBitmap(pResSet->m_arrImgId.GetAt(7));

  m_bmpDlgT.GetBitmap(&m_bmDlgT);
  m_bmpDlgB.GetBitmap(&m_bmDlgB);
  m_bmpDlgL.GetBitmap(&m_bmDlgL);
  m_bmpDlgR.GetBitmap(&m_bmDlgR);
  m_bmpDlgLT.GetBitmap(&m_bmDlgLT);
  m_bmpDlgLB.GetBitmap(&m_bmDlgLB);
  m_bmpDlgRT.GetBitmap(&m_bmDlgRT);
  m_bmpDlgRB.GetBitmap(&m_bmDlgRB);
  
  m_bImg = TRUE;
}

void CEXDialog::SetResSet(CEXBaseResSet *pResSet)
{
  m_pDialogResSet = (CEXDialogResSet *)pResSet;

  if(m_pDialogResSet->m_nType == DIALOG_CTRL)
    InitBitmap(m_pDialogResSet);

  m_pftText = m_pDialogResSet->m_font;
  m_crBkgnd = m_pDialogResSet->m_crBkColor;
  m_crText = m_pDialogResSet->m_crText;

}
void CEXDialog::SetDialogRegion(CWnd *pDlg)
{
  ASSERT(pDlg && IsWindow(pDlg->GetSafeHwnd()));
  if(m_bImg)
  {
    CRect rcClient;
    pDlg->GetClientRect(rcClient);

    CRgn rgnClient;
    rgnClient.CreateRectRgnIndirect(rcClient);

    CRgn rgnDiffLT;
    GetRegion(pDlg, rgnDiffLT, &m_bmpDlgLT, 0, 0);
    rgnClient.CombineRgn(&rgnClient, &rgnDiffLT, RGN_DIFF);

    CRgn rgnDiffRT;
    GetRegion(pDlg, rgnDiffRT, &m_bmpDlgRT, 
      rcClient.right-m_bmDlgRT.bmWidth, 0);

    rgnClient.CombineRgn(&rgnClient, &rgnDiffRT, RGN_DIFF);

    CRgn rgnDiffLB;
    GetRegion(pDlg, rgnDiffLB, &m_bmpDlgLB, 0, 
      rcClient.bottom-m_bmDlgLB.bmHeight);

    rgnClient.CombineRgn(&rgnClient, &rgnDiffLB, RGN_DIFF);

    CRgn rgnDiffRB;
    GetRegion(pDlg, rgnDiffRB, &m_bmpDlgRB, rcClient.Width()-m_bmDlgRB.bmWidth, 
      rcClient.Height()-m_bmDlgRB.bmHeight);

    rgnClient.CombineRgn(&rgnClient, &rgnDiffRB, RGN_DIFF);

    pDlg->SetWindowRgn(rgnClient, TRUE);
  }
}

void CEXDialog::GetRegion(CWnd * pWnd, CRgn & rgnDiff, CBitmap * pbmpSource, 
                          int nOffsetX, int nOffsetY)
{
  BITMAP bmInfo = {0};
  pbmpSource->GetBitmap(&bmInfo);

  CClientDC dc(pWnd);

  CDC dcMem;
  dcMem.CreateCompatibleDC(&dc);

  CBitmap* pOldBitmap = NULL;
  dcMem.SelectObject(pbmpSource);

  CRgn crRgnTmp;

  rgnDiff.DeleteObject();
  rgnDiff.CreateRectRgn(0, 0, 0, 0);

  COLORREF crTransparent = RGB(255, 0, 255);

  for (int i=0; i<bmInfo.bmHeight; i++)
  {
    for (int j=0; j<bmInfo.bmWidth; j++)
    {
      if (dcMem.GetPixel(j, i) == crTransparent)
      {
        crRgnTmp.CreateRectRgn(nOffsetX+j, nOffsetY+i, 
          nOffsetX+j+1, nOffsetY+i+1);

        rgnDiff.CombineRgn(&rgnDiff, &crRgnTmp, RGN_OR);
        crRgnTmp.DeleteObject();
      }
    }
  }

  dcMem.SelectObject(pOldBitmap);
}

void CEXDialog::DrawGroupBox(CWnd * pParentWnd, CWnd * pGroupBox, CDC * pDC)
{
  CRect rcGroupSetting;
  pGroupBox->GetWindowRect(rcGroupSetting);
  pParentWnd->ScreenToClient(rcGroupSetting);

  rcGroupSetting.InflateRect(1, -6, 0, 0);

  CString strText;
  pGroupBox->GetWindowText(strText);

  if (pGroupBox->IsWindowVisible())
    pGroupBox->ShowWindow(SW_HIDE);

  CFont * pOldFont = NULL;
  if(m_pftText)
    pOldFont = pDC->SelectObject(m_pftText);
  pDC->SetBkMode(TRANSPARENT);

  COLORREF crOldText = pDC->SetTextColor(m_crText);

  CSize siFont = pDC->GetTextExtent(strText);

  pDC->TextOut(rcGroupSetting.left+15, rcGroupSetting.top-(siFont.cy/2), 
    strText, strText.GetLength());

  pDC->SetTextColor(crOldText);
  pDC->SelectObject(pOldFont);

  COLORREF crPen1 = RGB(0,0,0);

  CPen * pOldPen = NULL;
  CPen penDark(PS_SOLID, 1, crPen1);
  CPen penBright(PS_SOLID, 1, RGB(122,122,122));

  pOldPen = pDC->SelectObject(&penDark);

  pDC->MoveTo(rcGroupSetting.left+7, rcGroupSetting.top);
  pDC->LineTo(rcGroupSetting.left, rcGroupSetting.top);
  pDC->LineTo(rcGroupSetting.left, rcGroupSetting.bottom-1);
  pDC->LineTo(rcGroupSetting.right-1, rcGroupSetting.bottom-1);
  pDC->LineTo(rcGroupSetting.right-1, rcGroupSetting.top);
  

  if(strText == L"")
  {
    pDC->LineTo(rcGroupSetting.left+siFont.cx+5, rcGroupSetting.top);
  }
  else
  {
    pDC->LineTo(rcGroupSetting.left+15+siFont.cx+5, rcGroupSetting.top);
  }

  pDC->SelectObject(pOldPen);

  pOldPen = pDC->SelectObject(&penBright);

  pDC->MoveTo(rcGroupSetting.left+7, rcGroupSetting.top+1);
  pDC->LineTo(rcGroupSetting.left+1, rcGroupSetting.top+1);
  pDC->LineTo(rcGroupSetting.left+1, rcGroupSetting.bottom-1);
  pDC->MoveTo(rcGroupSetting.left, rcGroupSetting.bottom);
  pDC->LineTo(rcGroupSetting.right, rcGroupSetting.bottom);
  pDC->LineTo(rcGroupSetting.right, rcGroupSetting.top-1);
  pDC->MoveTo(rcGroupSetting.right-2, rcGroupSetting.top+1);

  if(strText == L"")
  {
    pDC->LineTo(rcGroupSetting.left+siFont.cx+5, rcGroupSetting.top+1);
  }
  else
  {
    pDC->LineTo(rcGroupSetting.left+15+siFont.cx+5, rcGroupSetting.top+1);
  }

  pDC->SelectObject(pOldPen);
}

void CEXDialog::SetCaption(CString strCaption)
{
  m_strCaption = strCaption;
}

void CEXDialog::DrawDialog(CDialog *pDlg, CDC *pDC)
{
  CRect rcClient;
  pDlg->GetClientRect(rcClient);

  m_rtDlg = rcClient;

  CBrush brBkgnd(m_crBkgnd);
  pDC->FillRect(&rcClient, &brBkgnd);
  if(m_bImg)
  {
    CDC dcBmp;
    dcBmp.CreateCompatibleDC(pDC);

    CBitmap * pOldBmp = NULL;

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgLT);
    pDC->BitBlt(0, 0, m_bmDlgLT.bmWidth, m_bmDlgLT.bmHeight, 
      &dcBmp, 0, 0, SRCCOPY);

    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgT);
    pDC->StretchBlt(m_bmDlgLT.bmWidth, 0, rcClient.Width()-m_bmDlgLT.bmWidth -
      m_bmDlgRT.bmWidth, m_bmDlgT.bmHeight, &dcBmp, 0, 0, m_bmDlgT.bmWidth, 
      m_bmDlgT.bmHeight, SRCCOPY);

    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgRT);
    pDC->BitBlt(rcClient.right-m_bmDlgRT.bmWidth, 0,
      m_bmDlgRT.bmWidth, m_bmDlgLT.bmHeight, &dcBmp, 0, 0, SRCCOPY);
    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgL);
    pDC->StretchBlt(0, m_bmDlgLT.bmHeight, m_bmDlgL.bmWidth,
      rcClient.bottom-m_bmDlgLT.bmHeight-m_bmDlgLB.bmHeight,
      &dcBmp, 0, 0, m_bmDlgL.bmWidth, m_bmDlgL.bmHeight, SRCCOPY);
    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgR);
    pDC->StretchBlt(rcClient.right-m_bmDlgR.bmWidth, m_bmDlgRT.bmHeight,
      m_bmDlgR.bmWidth, rcClient.bottom-m_bmDlgRT.bmHeight-m_bmDlgRB.bmHeight,
      &dcBmp, 0, 0, m_bmDlgR.bmWidth, m_bmDlgR.bmHeight, SRCCOPY);
    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgLB);
    pDC->BitBlt(0, rcClient.bottom-m_bmDlgLB.bmHeight,
      m_bmDlgLB.bmWidth, m_bmDlgLB.bmHeight,
      &dcBmp, 0, 0, SRCCOPY);
    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgB);
    pDC->StretchBlt(m_bmDlgLB.bmWidth,
      rcClient.bottom-m_bmDlgB.bmHeight,
      rcClient.Width()-m_bmDlgLB.bmWidth-m_bmDlgRB.bmWidth,
      m_bmDlgB.bmHeight, &dcBmp, 0, 0, m_bmDlgB.bmWidth,
      m_bmDlgB.bmHeight, SRCCOPY);
    dcBmp.SelectObject(pOldBmp);

    pOldBmp = dcBmp.SelectObject(&m_bmpDlgRB);
    pDC->BitBlt(rcClient.right-m_bmDlgRB.bmWidth,
      rcClient.bottom-m_bmDlgRB.bmHeight,
      m_bmDlgRB.bmWidth, m_bmDlgRB.bmHeight, &dcBmp, 0, 0, SRCCOPY);
    dcBmp.SelectObject(pOldBmp);

    if (m_strCaption)
    {
      pDC->SetBkMode(TRANSPARENT);
      pDC->SetTextColor(m_crText);
      int nY = abs((m_bmDlgT.bmHeight-pDC->GetTextExtent(m_strCaption).cy) / 2);

      pDC->TextOut(25, nY, m_strCaption);
    }
  }
}

LRESULT CEXDialog::OnNcHitTest(CPoint point)
{
  m_rtDlg.bottom = m_bmDlgT.bmHeight;

  ScreenToClient(&point);

  if (m_rtDlg.PtInRect(point))
    return HTCAPTION;

  return CDialog::OnNcHitTest(point);
}
